package hsintiao.com.xintiaoAPP.heartfilter;

import java.util.ArrayList;

public class CanShuFilter {

    //T_h
    private  float T_h = 50;
    public float getT_h() {
        return T_h;
    }
    public void setT_h(float t_h) {
        T_h = t_h;
    }

    private  float tmax =0;

    private  float[] x=new float[8];//滤波使用
    private  float[] y=new float[8];//滤波使用
    public float[] getX() {
        return x;
    }
    public void setX(float[] x) {
        this.x = x;
    }
    public float[] getY() {
        return y;
    }
    public void setY(float[] y) {
        this.y = y;
    }

    private  float[] m_data=new float[10000];
    private  float m_iLBlind = 30;//
    double A0, A1, A2, A3, A4, A5, A6, A7, A8;
    double B0, B1, B2, B3, B4, B5, B6, B7, B8;
    private  int m_sample = 250;
    private  int RD=63;
    private  float[] m_Rawdata=new float[RD];

    public  void SetBasePara(float data, int num, int fillter, int sample, float max){
        if (num < 3 * sample)
        {
            if (data >  max){
                max=data;
                tmax = max;
            }
        }
        else
        {
            float maxdata = max;
            float type = 1;
            if (fillter == 20)//使用20HZ滤波器
            {
                type = 0.3f;

            }
            else if (fillter == 40)//使用40HZ滤波器
            {
                type = 0.15f;
            }
            else
            {
                type = 1;
            }
            T_h = SetPara(maxdata, type);
            //System.out.println("th="+T_h);

        }
    }
    public static float SetPara(float data, float type){
        float t_para = 0.005f;
        if (data < 0.01 * type)   //电压低于0.4mv异常佩戴数据
        {
            return -1;
        }
        else if (data < 1 * type && data>0.01 * type)
        {
            t_para = 0.001f;
        }
        else if (data <= 2 * type && data >= 1 * type)
        {
            t_para = 0.01f;
        }
        else if (data <= 3 * type && data > 2 * type)
        {
            t_para = 0.2f;
        }
        else
        {
            t_para = 0.01f;
            //t_para = data * (1 - type);
        }
        return t_para;
    }



    public void domain(ArrayList<String> sdata){
        float data = 0.0f;
        int j = 0;
        int Hr = 0;
        //测试
        int filemax = (int) (sdata.size());
        //int filemax = (int) (3000);
        int len = (int) (sdata.size());
        //int len = (int) (3000);
        //int m_sample = 100;
        float[] m_filterdata=new float[sdata.size()];
        //float[] m_filterdata=new float[(int) (3000)];
        int pBMP = 0;
        float m_rr = 0.0f;
        //int k = 0;

        x=new float[8];
        y=new float[8];
        //m_filterdata=new float[sdata.size()];
        //InifData();
        ReadData(sdata); //测试时数据读取
        //SetStaticPara();

        ////滤波器的使用

        for(int i=0; i < 8; i++)
        {
            x[i] = m_data[i];
            y[i] = 0;
            m_filterdata[i] = 0;
        }

        for(int i=8; i < filemax; i++)
        {
            x[0] = x[1]; x[1] = x[2];
            x[2] = x[3]; x[3] = x[4];
            x[4] = x[5]; x[5] = x[6];
            x[6] = x[7];
            x[7] = m_data[i];

            y[0] = y[1]; y[1] = y[2];
            y[2] = y[3]; y[3] = y[4];
            y[4] = y[5]; y[5] = y[6];
            y[6] = y[7];

            m_filterdata[i] = Filter(m_sample, y, x);  //%采样率支持100, 125, 250, 500

            y[7] = m_filterdata[i];
            //m_filterdata[i] = m_data[i];
        }



        //方法验证
        for (int i = 0; i < 16; i++)
        {
            m_Rawdata[i] = m_filterdata[i];

        }
        //float tmax = 0;
        for (int i = 16; i < filemax; i++)
        {
            //前3秒学习参数
            SetBasePara(m_filterdata[i], i, 0, m_sample, tmax);
            //System.out.println(T_h);

            //3秒后开始计算
            /*if (i > 3 * m_sample)
            {
                if(i == 751){
                    System.out.println("最后tmax= "+tmax);
                }
                //EventFlag t = Computing_Interface(m_filterdata[i], m_sample, k);
                mevent = Computing_Interface(m_filterdata[i], m_sample, k);
                //return t;
            }*/
        }
        //return null;
    }

    //数据初始化
    /*void InifData()
    {
        // 初始化全局变量
        Count_N = 0;
        m_peak = 0;
        prepeak_pos = 0;
        curpeak_pos = 0;
        // 初始化变量
        m_data=new float[3000];
        pSdata=new float[PS];
        Smooth=new float[S];
        m_Rawdata=new float[RD];
        //用于事件判断
        m_rate=new int[HRN];
        m_RR=new int[HRN];
    }*/

    void ReadData(ArrayList<String> sdata)
    {
        ArrayList<String> arrayList = sdata;
        // 对ArrayList中存储的字符串进行处理
        int length = arrayList.size();
        float[] array = new float[length];
        for (int i = 0; i < length; i++) {
            String s = arrayList.get(i);
            if(s == null || s.trim().length()==0){
                System.out.println("i="+i);
            }
            array[i] = Float.parseFloat(s);
        }
        m_data=array;
        // 返回数组
    }

    /*void SetStaticPara()
    {
        polycardia = 120;//心动过速参数
        pBrady = 40;//心动过缓参数
        pasystole = 3000;//停搏
        pauseRR = 2000;//长RR间期
        arrrate = 20;

    }*/

    public float Filter(int sample,float[] y,float[] x){
        float data = 0.0f;
        m_iLBlind = 0.25f * sample;
        switch (sample)
        {
            case 100:
            {
                B0 = 0.004842115239347f;
                B1 = 0.033894806675427f;
                B2 = 0.101684420026280f;
                B3 = 0.169474033377133f;
                B4 = 0.169474033377133f;
                B5 = 0.101684420026280f;
                B6 = 0.033894806675427f;
                B7 = 0.004842115239347f;

                A0 = -1.389280147157137f;
                A1 = 1.675023617528826f;
                A2 = -1.053897316375457f;
                A3 = 0.508551510935084f;
                A4 = -0.144829453299512f;
                A5 = 0.026252219253809f;
                A6 = -0.002029680249244f;

                break;
            }
            case 125:
            {
                B0 = 0.0013890500969125303f;
                B1 = 0.0097233506783877127f;
                B2 = 0.029170052035163138f;
                B3 = 0.048616753391938564f;
                B4 = 0.048616753391938564f;
                B5 = 0.029170052035163138f;
                B6 = 0.0097233506783877127f;
                B7 = 0.0013890500969125303f;

                A0 = -2.5033956673117279f;
                A1 = 3.3824966598829622f;
                A2 = -2.7642649121742515f;
                A3 = 1.4676105985961547f;
                A4 = -0.4926085537872904f;
                A5 = 0.096335020543948865f;
                A6 = -0.0083747333449916868f;

                break;
            }
            case 250:
            {
                B0 = 0.00002340795342087028f;
                B1 = 0.00016385567394609196f;
                B2 = 0.00049156702183827592f;
                B3 = 0.00081927836973045979f;
                B4 = 0.00081927836973045979f;
                B5 = 0.00049156702183827592f;
                B6 = 0.00016385567394609196f;
                B7 = 0.00002340795342087028f;

                A0 = -4.7442505906284662f;
                A1 = 9.9102177287216797f;
                A2 = -11.753892913717335f;
                A3 = 8.5195541458787289f;
                A4 = -3.7645939793303058f;
                A5 = 0.93721996649662698f;
                A6 = -0.10125813785269747f;

                break;
            }
            case 500:
            {
                B0 = 0.00000029136561169850398f;
                B1 = 0.0000020395592818895277f;
                B2 = 0.000006118677845668583f;
                B3 = 0.000010197796409447639f;
                B4 = 0.000010197796409447639f;
                B5 = 0.000006118677845668583f;
                B6 = 0.0000020395592818895277f;
                B7 = 0.00000000000000259454f;

                A0 = -5.8709586003642285f;
                A1 = 14.850464194077391f;
                A2 = -20.970429631109958f;
                A3 = 17.847388502845412f;
                A4 = -9.1517654085969617f;
                A5 = 2.6173083471483545f;
                A6 = -0.32197010920171321f;

                break;
            }
        }
        y[7] = (float) (B0 * x[7] + B1 * x[6] + B2 * x[5] + B3 * x[4] + B4 * x[3] + B5 * x[2] + B6 * x[1] + B7 * x[0] -
                A0 * y[6] - A1 * y[5] - A2 * y[4] - A3 * y[3] - A4 * y[2] - A5 * y[1] - A6 * y[0]);

        data = y[7];
        return data;
    }

}
